package com.ssy.lingxi.search.controller.pc.channel;

import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.search.api.model.request.SimpleIdListRequest;
import com.ssy.lingxi.search.api.model.request.channel.ChannelPurchaseRequest;
import com.ssy.lingxi.search.entity.channel.PurchaseChannel;
import com.ssy.lingxi.search.model.response.PurchaseChannelResponse;
import com.ssy.lingxi.search.service.IPurchaseChannelService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @menu: 渠道商城--进货单管理
 * 进货单管理类
 * @author jw.chen
 * @version 2.0.0
 * @date 2020/9/4
 */
@RestController
@RequestMapping("/search/shop/purchaseChannel")
public class PurchaseChannelController extends BaseController {

    @Resource
    private IPurchaseChannelService purchaseChannelService;

    @Resource
    private HttpServletRequest request;

    /**
     * 查询进货单列表
     * @param channelMemberId 渠道会员id
     * @param channelMemberRoleId 渠道会员角色id
     * @return
     */
    @RequestMapping(value = "/getPurchaseList", method = RequestMethod.GET)
    public Wrapper<List<PurchaseChannelResponse>> getPurchaseList(@RequestParam("channelMemberId") Long channelMemberId, @RequestParam("channelMemberRoleId") Long channelMemberRoleId) {
        UserLoginCacheDTO sysUser = this.getSysUser(request);
        Long shopId = Long.parseLong(request.getHeader("shopId"));
        return Wrapper.success(purchaseChannelService.getPurchaseList(sysUser, channelMemberId, channelMemberRoleId, shopId));
    }

    /**
     * 新增/修改进货单
     * @param purchaseChannelRequest 进货单实体
     * @return 进货单数量
     */
    @RequestMapping(value = "/saveOrUpdatePurchase", method = RequestMethod.POST)
    public Wrapper<Integer> saveOrUpdatePurchase(@RequestBody ChannelPurchaseRequest purchaseChannelRequest){
        UserLoginCacheDTO sysUser = this.getSysUser(request);
        Long shopId = Long.parseLong(request.getHeader("shopId"));
        PurchaseChannel purchaseChannel = this.modelMapper.map(purchaseChannelRequest, PurchaseChannel.class);
        purchaseChannel.setShopId(shopId);
        return Wrapper.success(purchaseChannelService.saveOrUpdatePurchase(sysUser, purchaseChannel));
    }

    /**
     * 删除进货单
     * @param simpleIdListRequest
     * @return
     */
    @RequestMapping(value = "/deletePurchase", method = RequestMethod.POST)
    public Wrapper<String> deletePurchase(@RequestBody SimpleIdListRequest simpleIdListRequest){
        return Wrapper.success(purchaseChannelService.deletePurchase(simpleIdListRequest));
    }

    /**
     * 查询进货单数量
     * @param channelMemberId 渠道会员id
     * @param channelMemberRoleId 渠道会员角色id
     */
    @RequestMapping(value = "/getPurchaseCount", method = RequestMethod.GET)
    public Wrapper<Integer> getPurchaseCount(@RequestParam("channelMemberId") Long channelMemberId, @RequestParam("channelMemberRoleId") Long channelMemberRoleId){
        UserLoginCacheDTO sysUser = this.getSysUser(request);
        Long shopId = Long.parseLong(request.getHeader("shopId"));
        return Wrapper.success(purchaseChannelService.getPurchaseCount(sysUser, shopId, channelMemberId, channelMemberRoleId));
    }

}
